poj 1077 Eight

  经典的八数码问题,A*启发搜索和IDA*都可以解决。其实八数码问题只有 9!=3.6*10^5 种情况,好像可以打表?并且双向dfs应该也是可以过的。比较好的做法是A*和IDA*。

  Github: https://github.com/wmpeng/Algorithm

  A*就是有一个对于每一种情况有一个评估f=g+h,f为评估的值,g是走到现在的情况走了多少步,h是从当前的情况走到答案所需要的步数。h是一个估值函数,函数的设计对求解的效率起到至关重要的作用,选取的不好甚至会差于普通的搜索。对于每种情况有了f,那么每次搜索都从已经找的情况中f最小的开始搜索。实现方法就是把初始情况先push进优先队列,然后每次取top,把top能演变出的四个情况在push进队列。中间注意不要重复入列,队列为空时代表没有结果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string>
#include<string.h>
using namespace std;
int list[9];
int jc[9] = { 1,1,2,6,24,120,720,5040,40320 };
int ansid = 322560;
bool v[400000];
struct rec
{
int id;
int list[9];
int g;
int h;
string ans;
rec operator=(rec _r)
{
id = _r.id;
for (int i = 0; i < 9; i++)
list[i] = _r.list[i];
g = _r.g;
h = _r.h;
ans = _r.ans;
return *this;
}
rec() {}
rec(int _id, int* _list, int _g, int _h, string _ans)
{
id = _id;
for (int i = 0; i < 9; i++)
list[i] = _list[i];
g = _g;
h = _h;
ans = _ans;
}
};

template<class T> T abs(T x)
{
return (x < 0 ? -x : x);
}
bool operator<(rec r1, rec r2)
{
return r1.h + r1.g > r2.h + r2.g;
}

int hash_(int list[], int len = 9)
{
int ans = 0;
for (int i = 0; i < len; i++)
{
int cnt = 0;
for (int j = 0; j < i; j++)
if (list[i] < list[j])
cnt++;
ans += cnt * jc[i];
}
return ans;
}
int eval_(int list[])
{
int ans = 0;
for (int i = 0; i < 9; i++)
{
if (list[i] == 0)
{
continue;
int h1, l1, h2, l2;
h1 = i / 3;
l1 = i % 3;
h2 = 2;
l2 = 2;
ans += abs(h1 - h2) + abs(l1 - l2);
}
else
{
int h1, l1, h2, l2;
h1 = i / 3;
l1 = i % 3;
h2 = (list[i] - 1) / 3;
l2 = (list[i] - 1) % 3;
ans += abs(h1 - h2) + abs(l1 - l2);
}
}
return ans;
}

rec up(rec curr)
{
for (int i = 0; i < 9; i++)
if (curr.list[i] == 0)
{
if (i / 3 != 0)
{
curr.list[i] ^= curr.list[i - 3] ^= curr.list[i] ^= curr.list[i - 3];
return rec(hash_(curr.list), curr.list, curr.g + 1, eval_(curr.list), curr.ans + 'u');
}
else
return curr;
}
}
rec dn(rec curr)
{
for (int i = 0; i < 9; i++)
if (curr.list[i] == 0)
{
if (i / 3 != 2)
{
curr.list[i] ^= curr.list[i + 3] ^= curr.list[i] ^= curr.list[i + 3];
return rec(hash_(curr.list), curr.list, curr.g + 1, eval_(curr.list), curr.ans + 'd');
}
else
return curr;
}
}
rec lft(rec curr)
{
for (int i = 0; i < 9; i++)
if (curr.list[i] == 0)
{
if (i % 3 != 0)
{
curr.list[i] ^= curr.list[i - 1] ^= curr.list[i] ^= curr.list[i - 1];
return rec(hash_(curr.list), curr.list, curr.g + 1, eval_(curr.list), curr.ans + 'l');
}
else
return curr;
}
}
rec rt(rec curr)
{
for (int i = 0; i < 9; i++)
if (curr.list[i] == 0)
{
if (i % 3 != 2)
{
curr.list[i] ^= curr.list[i + 1] ^= curr.list[i] ^= curr.list[i + 1];
return rec(hash_(curr.list), curr.list, curr.g + 1, eval_(curr.list), curr.ans + 'r');
}
else
return curr;
}
}

void prt(rec r)
{
for (int i = 0; i < r.ans.length(); i++)
cout << r.ans[i];
cout << endl;
}
void prtmat(int list[])
{
for (int i = 0; i <= 8; i++)
{
cout << list[i];
if ((i + 1) % 3 == 0)
cout << endl;
}
cout << endl;
}
void bh(char c)
{
for (int i = 0; i < 9; i++)
if (list[i] == 0)
{
if (c == 'd')
list[i] ^= list[i + 3] ^= list[i] ^= list[i + 3];
else if (c == 'u')
list[i] ^= list[i - 3] ^= list[i] ^= list[i - 3];
else if (c == 'r')
list[i] ^= list[i + 1] ^= list[i] ^= list[i + 1];
else if (c == 'l')
list[i] ^= list[i - 1] ^= list[i] ^= list[i - 1];
return;
}

}
int main()
{
int cnt = 0;
int i, j;
char c;
for (i = 0; i < 9; i++)
{
cin >> c;
if (c == 'x')
list[i] = 0;
else
list[i] = c - '0';
}
priority_queue<rec> que;
que.push(rec(hash_(list), list, 0, eval_(list), ""));
v[hash_(list)] = true;
while (!que.empty())
{
rec curr = que.top();
que.pop();
if (curr.id == ansid)
{
prt(curr);
break;
}
rec temp;
temp = up(curr);
if (!v[temp.id])
{
v[temp.id] = true;
que.push(temp);
cnt++;
}
temp = dn(curr);
if (!v[temp.id])
{
v[temp.id] = true;
que.push(temp);
cnt++;
}
temp = lft(curr);
if (!v[temp.id])
{
v[temp.id] = true;
que.push(temp);
cnt++;
}
temp = rt(curr);
if (!v[temp.id])
{
v[temp.id] = true;
que.push(temp);
cnt++;
}
}
cout << endl << cnt << endl;

return 0;
}

  IDA*算法就是一个限制搜索深度的dfs,在突破一定条件时时候就不要继续搜索了,如果没有找到答案就放宽限制条件。代码实现上IDA*更加简单,就是dfs加上一个限制,不需要像A*一样记录找过的情况。
注意h函数的设计,不能越接近实际效率更高,但是注意h不能大于实际的情况,因为如果h大于实际可能会导致搜索不到结果。

  这道题我的方法:限制条件时g+h的和不大于某个数,然后不断方法这个限制直到搜索到结果。注意不要来回走的剪枝。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
#include<iostream>
#include<stdio.h>
#include<queue>
#include<string>
#include<string.h>
using namespace std;
int list[9];
int step[128];
int deep = 1;
int jc[9] = { 1,1,2,6,24,120,720,5040,40320 };
int mi;
char ans[100];
int cnt=0;

template<class T> T abs(T x)
{
return (x < 0 ? -x : x);
}
int hash_(int list[])
{
int ans = 0;
for (int i = 0; i < 9; i++)
{
int cnt = 0;
for (int j = 0; j < i; j++)
if (list[i] < list[j])
cnt++;
ans += cnt * jc[i];
}
return ans;
}
int eval_(int list[])
{
int ans = 0;
for (int i = 0; i < 9; i++)
{
if (list[i] == 0)
{
continue;
int h1, l1, h2, l2;
h1 = i / 3;
l1 = i % 3;
h2 = 2;
l2 = 2;
ans += abs(h1 - h2) + abs(l1 - l2);
}
else
{
int h1, l1, h2, l2;
h1 = i / 3;
l1 = i % 3;
h2 = (list[i] - 1) / 3;
l2 = (list[i] - 1) % 3;
ans += abs(h1 - h2) + abs(l1 - l2);
}
}
return ans;
}
bool up(int* list,int* temp)
{
for (int i = 0; i < 9; i++)
temp[i] = list[i];
for (int i = 0; i < 9; i++)
if (temp[i] == 0)
{
if (i / 3 != 0)
{
temp[i] ^= temp[i - 3] ^= temp[i] ^= temp[i - 3];
return true;
}
else
return false;
}
}
bool dn(int* list, int* temp)
{
for (int i = 0; i < 9; i++)
temp[i] = list[i];
for (int i = 0; i < 9; i++)
if (temp[i] == 0)
{
if (i / 3 != 2)
{
temp[i] ^= temp[i + 3] ^= temp[i] ^= temp[i + 3];
return true;
}
else
return false;
}
}
bool lft(int* list, int* temp)
{
for (int i = 0; i < 9; i++)
temp[i] = list[i];
for (int i = 0; i < 9; i++)
if (temp[i] == 0)
{
if (i % 3 != 0)
{
temp[i] ^= temp[i - 1] ^= temp[i] ^= temp[i - 1];
return true;
}
else
return false;
}
}
bool rt(int* list, int* temp)
{
for (int i = 0; i < 9; i++)
temp[i] = list[i];
for (int i = 0; i < 9; i++)
if (temp[i] == 0)
{
if (i % 3 != 2)
{
temp[i] ^= temp[i + 1] ^= temp[i] ^= temp[i + 1];
return true;
}
else
return false;
}
}

bool finish(int list[])
{
for (int i = 0; i < 8; i++)
if (list[i] != i + 1)
return false;
if (list[8] != 0)
return false;
return true;
}
void prt(char *ans)
{
for (int i = 0; ans[i] != 0;i++)
cout << ans[i];
cout << endl;
}

bool dfs(int *_list, int currDp, int pre)
{
if (finish(_list))
{
prt(ans);
return true;
}
int h = eval_(_list);
if (h < mi)
mi = h;

if (currDp+ h> deep)
return false;
int list[9];

if (step['u'] + pre != 0 && up(_list, list))//如果前一步和后一步相反就不要走了
{
ans[currDp] = 'u';
if (dfs(list, currDp + 1, step['u']))
return true;
}
if (step['d'] + pre != 0 && dn(_list, list))
{
ans[currDp] = 'd';
if (dfs(list, currDp + 1, step['d']))
return true;
}
if (step['l'] + pre != 0 && lft(_list, list))
{
ans[currDp] = 'l';
if (dfs(list, currDp + 1, step['l']))
return true;
}
if (step['r'] + pre != 0 && rt(_list, list))
{
ans[currDp] = 'r';
if (dfs(list, currDp + 1, step['r']))
return true;
}
return false;
}
void init()
{
step['u'] = 1;
step['d'] = -1;
step['l'] = 2;
step['r'] = -2;
}

int main()
{
init();
int i, j;
char c;
for (i = 0; i < 9; i++)
{
cin >> c;
if (c == 'x')
list[i] = 0;
else
list[i] = c - '0';
}

while (1)
{
mi = 0x7fffffff;
memset(ans, 0, sizeof(ans));
if (dfs(list, 0, 0))
break;
deep+=mi;
if (deep >= 50)
{
cout << "unsolvable" << endl;
break;
}
}

return 0;
}